<?php

namespace App\Http\Api\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

use Dingo\Api\Routing\Helpers;

use App\Repositories\Mgt\CategoryRepository as Category;

class UtilController extends Controller
{
    use Helpers;
    
    protected $guard = 'api';

    protected $category;
    
    public function __construct(Category $category)
    {
        parent::__construct();

        $this->category = $category;
    }

    /**
     * Create a new AuthController instance.
     *
     * @return void
     */
    /*public function __construct()
    {
        $this->middleware('auth:api', ['except' => ['login']]);
    }*/

    public function category()
    {
        $categories = $this->category->index();

        return $categories;
    }


    public function test()
    {
        return ($this->guard()->user());
    }

    /**
     * Get a JWT token via given credentials.
     *
     * @param  \Illuminate\Http\Request  $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function login(Request $request)
    {
        $token = $this->guard()->attempt([
            'mobile' => $request->mobile, 
            'password' => $request->passwd, 
            'status' => STATUS_NORMAL
            ]);
            
        if (!!$token )
            //return $request->headers->set('Authorization', 'Bearer ' .$token);
            return $this->respondWithToken($token);
       

        /*
        $credentials = $request->only('mobile', 'passwd');

        if ($token = $this->guard($this->guard)->attempt($credentials)) {
            return $this->respondWithToken($token);
        }*/

        return $this->response->errorUnauthorized();
    }

    /**
     * Result will automatically format the response as JSON and set the Content-Type header to application/json.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function me()
    {
        return $this->guard()->user();
    }

    /**
     * Log the user out (Invalidate the token)
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function logout()
    {
        $this->guard()->logout();

        //return response()->json(['message' => 'Successfully logged out']);
        return $this->response->array(['message' => '退出成功']);
    }

    /**
     * Refresh a token.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function refresh()
    {
        return $this->respondWithToken($this->guard()->refresh());
    }

    /**
     * Get the token array structure.
     *
     * @param  string $token
     *
     * @return \Illuminate\Http\JsonResponse
     */
    protected function respondWithToken($token)
    {
        return response()->json([
            'access_token' => $token,
            'token_type' => 'bearer',
            'expires_in' => $this->guard()->factory()->getTTL() * 60
        ]);
    }

    /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\Guard
     */
    public function guard()
    {
        return Auth::guard($this->guard);
    }
}